package ClientUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import Protocol.noticeMsgWord.NoticeMsgWord;
import Protocol.noticeMsgWord.NoticeMsgWord_R;
import Protocol.user.BasePersonInfo;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ContactJDialog extends javax.swing.JDialog {
	private JSeparator jSeparator;
	private JPanel chatting;
	private JTextArea chattingInputArea;
	private JButton sendButton;
	private JTextArea chattingHistoryArea;
	private NoticeMsgWord	chatMsg ;
	private ClientGUI		window ;
	private JScrollPane jScrollPane1;
	private BasePersonInfo	target, self;

	public ContactJDialog(JFrame frame, ClientGUI  cgui, BasePersonInfo  targetBpi) {
		super(frame);
		window = cgui;
		target = targetBpi;
		self = cgui.getClient().getClientData().getSelf_basic();
		initGUI();
		addListeners();
	}
	public BasePersonInfo	getTargetBpi() {
		return target;
	}
	public void setCurrentMsg(NoticeMsgWord_R   cmr) {
		this.chatMsg = cmr.getChatMsgList().get(0);
		this.appendMsg();
	}
	private void addListeners() {
		sendButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				collectMsg();
				appendMsg();
				sendMsg();
			}
			
		});
	}
	private void collectMsg() {
		chatMsg   = new NoticeMsgWord();
		chatMsg.setBody(this.chattingInputArea.getText());
		chatMsg.setFrom_id(self.getUser_id());
		chatMsg.setTo_id("user:"+target.getUser_id());
	}
	private  void appendMsg() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if(chatMsg.getFrom_id() == self.getUser_id()) {
					chattingHistoryArea.append(self.getName()+":\n     "+chatMsg.getBody()+"\n");
				}					
				else if(chatMsg.getFrom_id() == target.getUser_id()) {
					chattingHistoryArea.append(target.getName()+":\n     "+chatMsg.getBody()+"\n");
				}
				//chattingHistoryArea.repaint();
			}
		});
	}
	private void sendMsg() {
		NoticeMsgWord_R	cm_r = new NoticeMsgWord_R();
		ArrayList<NoticeMsgWord>    chatList = new ArrayList<NoticeMsgWord>();
		chatList.add(chatMsg);
		cm_r.setChatMsgList(chatList);
		window.getClient().getClientNet().sendMsgTo(window.getClient().getClientData().getServer_basic(), cm_r);
	}
	private void thisWindowClosed() {
		this.window.mainFrame.realContactPanel.getChatDialogList().remove(this);
	}
	private void initGUI() {
		try {
			this.setSize(482, 611);
			this.setTitle(self.getName() + " talking to "+ target.getName());
			{
				getContentPane().setLayout(null);
				{
					jSeparator = new JSeparator();
					getContentPane().add(jSeparator);
					jSeparator.setBounds(12, 63, 436, 10);
				}
				{
					chatting = new JPanel();
					getContentPane().add(chatting);
					chatting.setBounds(39, 109, 379, 418);
					chatting.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
					chatting.setLayout(null);
					{
						jScrollPane1 = new JScrollPane();
						chatting.add(jScrollPane1);
						jScrollPane1.setBounds(40, 20, 288, 251);
						{
							chattingHistoryArea = new JTextArea();
							jScrollPane1.setViewportView(chattingHistoryArea);
							chattingHistoryArea.setBounds(40, 26, 285, 232);
						}
					}
					{
						chattingInputArea = new JTextArea();
						chatting.add(chattingInputArea);
						chattingInputArea.setText("your words here");
						chattingInputArea.setBounds(46, 310, 288, 44);
					}
					{
						sendButton = new JButton();
						chatting.add(sendButton);
						sendButton.setText("Send");
						sendButton.setBounds(131, 371, 129, 24);
					}
				}
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
